package com.benjaminwan.read.free.config

import com.benjaminwan.read.free.R
import com.benjaminwan.read.free.base.viewmodel.state
import com.benjaminwan.read.free.config.Options.optionLoginQuestion
import com.benjaminwan.read.free.model.ItemConfig
import com.benjaminwan.read.free.model.enums.ItemType
import com.benjaminwan.read.free.model.listOfItemConfig
import com.benjaminwan.read.free.ui.net.NetViewModel
import com.benjaminwan.read.free.utils.*
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView

object LoginConfig {
    fun getLoginConfig(netVM: NetViewModel): List<ItemConfig> {
        return listOfItemConfig {
            itemConfig {
                type = ItemType.Text
                titleStr = "用户名"
                rightTextFunc = { netVM.state.username }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 0, -1, null,
                        { dialog, which ->
                            val default = ""
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        netVM.setUserName(input)
                        dialog.dismiss()
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "密码"
                rightTextFunc = { getNumOfChar('*', netVM.state.password.length) }
                click = { view ->
                    val hint = "请输入${title}"
                    view.context.showPasswordInputDialog(
                        title, hint, netVM.state.password, 0, -1,
                        null, getString(R.string.dialog_clear),
                        { dialog, which ->
                            dialog.inputEditText?.setText("")
                            dialog.inputEditText?.setSelection("".length)
                        },
                        { dialog, _ ->
                            val input = dialog.inputEditText?.text.toString()
                            netVM.setPassword(input)
                            dialog.dismiss()
                        })
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "安全提问"
                rightTextFunc = { getOptionName(optionLoginQuestion, netVM.state.questionId) }
                click = { view ->
                    (view as SuperTextView).showPicker(
                        title, optionLoginQuestion
                    ) { opValue, opName ->
                        if (opName == view.rightString) return@showPicker
                        netVM.setQuestionId(opValue)
                        if (opValue == 0) {
                            netVM.setAnswer("")
                        }
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "回答"
                rightTextFunc = { netVM.state.answer }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 0, -1, null,
                        { dialog, which ->
                            val default = ""
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        netVM.setAnswer(input)
                        dialog.dismiss()
                    }
                }
            }
        }
    }
}